package com.spzx.common.feign;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 定义feign拦截器,解决远程调用feign传递token值，将请求头传入token
 * <p>
 * Project: spzx-parent
 * Package: com.spzx.common.feign
 * Version: 1.0
 * <p>
 * Created by  wangjiaxin  on 2024/4/29 下午11:33
 */
public class UserTokenFeignInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String token = request.getHeader("token");
        //远程调用时将token传递过去
        requestTemplate.header("token", token);
    }

}
